<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\models\UserConfig;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $user app\models\User
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '用户详情';
$this->params['breadcrumbs'][] = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<table class="table table-striped table-bordered table-hover">
    <tr>
        <th colspan="2"><h4>基本信息</h4></th>
    </tr>
    <tr>
        <th>编号</th>
        <td><?php echo $user->id;?></td>
    </tr>
    <tr>
        <th>登录用户名</th>
        <td><?php echo Html::encode($user->username);?></td>
    </tr>
    <tr>
        <th>登录密码</th>
        <td id="reset_password"><button class="btn btn-sm btn-warning" onclick="resetUserPassword(<?php echo $user->id;?>)">重置密码</button></td>
    </tr>
    <tr>
        <th>状态</th>
        <td><?php echo KeyMap::getValue('user_status', $user->status);?></td>
    </tr>
    <tr>
        <th>创建时间</th>
        <td><?php echo Yii::$app->formatter->asDatetime($user->create_time);?></td>
    </tr>
    <tr>
        <th colspan="2"><h4>账户</h4></th>
    </tr>
    <tr>
        <th>付费方式</th>
        <td><?php $prepaidPostpaid = UserConfig::getConfig($user->id, 'prepaid_postpaid', '');
            echo ['' => '', 'prepaid' => '预付费', 'postpaid' => '后付费'][$prepaidPostpaid];?>
        </td>
    </tr>
    <tr>
        <th>余额</th>
        <td><?php echo $user->account->money;?></td>
    </tr>
</table>
<script>
    /**
     * 重置用户密码
     * @param id 用户编号
     */
    function resetUserPassword(id) {
        $.getJSON('<?php echo Url::to(['/agent/user/reset-user-password']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('密码已重置。');
                $('#reset_password').html(json['new_password']);
            }
        });
    }
</script>
